import time

import serial
ser=serial.Serial('/dev/ttyUSB0',115200,timeout=0.5)
def ser_read_demo1():

    t1=time.time()

    flag=25
    if ser.is_open:
        while flag:
            data=ser.read(6)
            if data==b' rssi=':
                d1=ser.read(5)
                # print(d1[-3::])
                if d1[-3::]==b'agc':
                    print(data+d1[0:2])
                    print(f'demo1: {25-flag}: {time.time()-t1}')
                    flag-=1
                    t1=time.time()
            if time.time()-t1>25:
                t1=time.time()
                flag-=1
                print(f'demo1: {25 - flag}: read false')


def ser_read_demo2():
    t1=time.time()

    flag=1
    if ser.is_open:
        while flag:
            data=ser.read(33)
            if b'rssi=' in data and b'agc' in data:
                data=data.decode()
                ind=data.find('rssi')
                if len(data)>=ind+9:

                    data=data[ind+5:ind+9]
                    print(f'{time.time()} : rssi={data}')
            # print(data)
            # data=ser.read(3)
            # print(data)
            # if data==b'rx0':
            #     d1=ser.read(43)
            #     print(d1[35:42])
            #     print(f'demo2: {25 - flag}: {time.time() - t1}')
            #     flag -= 1
            #     t1 = time.time()

                # print(data.decode())

def test_time():
    try:
        ser_read_demo1()
        ser_read_demo2()
    except Exception as e:
        ser.close()
        print(e)
        print('serial error closed')
ser_read_demo2()